﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_Address : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["UserName"] == null)
        {
            Response.Write("<script>alert('请先登录!');window.location.href='../Login.aspx'</script>");
        }
        else
        {
            if (!IsPostBack)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con.Open();
                string sql = "select * from Branch";
                SqlCommand com = new SqlCommand(sql, con);
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    DropDownList1.Items.Add(new ListItem(dr["Branch"].ToString(), dr["Dutyid"].ToString()));
                }
                DropDownList1.Items.Insert(0, "全部");
                con.Close();
            }
        }
        Bind();
    }
    public void Bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();

        string sql = "select * from Employee";
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        this.Label1.Text = "共" + ds.Tables[0].Rows.Count + "条记录";
        con.Close();
    }
    protected void cmdAdd_Click(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {


    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem.Text == "全部")
        {
            if (this.TextBox1.Text.Trim() == "")
            {
                Response.Write("<script>alert('请输入查询条件！')</script>");
            }
            else
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con.Open();
                string Name = this.TextBox1.Text.Trim();
                string sql = "select * from Employee where name like('%" + Name + "%') ";
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    Response.Write("<script>alert('对不起,没有你查找的员工！')</script>");
                }
                else
                {
                    GridView1.DataSource = ds;

                    GridView1.DataBind();
                    this.Label1.Text = "共" + ds.Tables[0].Rows.Count + "条信息";
                    con.Close();
                }
            }
        }
        else
        {
            if (this.TextBox1.Text.Trim() == "")
            {
                Response.Write("<script>alert('请输入查询条件！')</script>");
            }
            else
            {
                int Dutyid = Convert.ToInt32(DropDownList1.SelectedItem.Value.ToString());
                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con.Open();
                string Name = this.TextBox1.Text.Trim();
                string sql = "select * from Employee where name like('%" + Name + "%') and Dutyid=" + Dutyid + "";
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    Response.Write("<script>alert('对不起,没有你查找的员工！')</script>");
                }
                else
                {
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                    this.Label1.Text = "共" + ds.Tables[0].Rows.Count + "条信息";
                }
                con.Close();
            }
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string Employeeid = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        Response.Redirect("../Info/Restinfo.aspx?Employeeid=" + Employeeid + "");
    }
    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
    }
    protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {


    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int Employeeid = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Response.Redirect("AddRest.aspx?Employeeid=" + Employeeid + "");
    }
}
